#include "GAF_Animation.h"
#include "GAF_Platform.h"

namespace GAF
{
	Animation::Animation()
	{
		ATTRIB(Interval)	= 0;
		m_pTimer		= NULL;
		m_isExactTimer		= false;
	}

	Animation::~Animation()
	{
		if(m_pTimer != NULL)
			GAF_DELETE(m_pTimer);
	}

	void Animation::ExactTimer()
	{
		m_isExactTimer = true;
	}

	bool Animation::Playing(Bundle* pParam)
	{
		if(m_pTimer == NULL)
		{
			m_pTimer = GAF_NEW(Timer);
		}

		m_pTimer->SetRunClass(this);
		if(m_isExactTimer)
			m_pTimer->Exact();
		m_pTimer->SetInterval(ATTRIB(Interval));

		if(!m_pTimer->Start())
		{
			return false;
		}

		m_ttLast = GAF_pTHEPLATFORM->GetTimeTicks();

		return true;
	}

	bool Animation::Pauseing()
	{
		GAF_ASSERT_R(m_pTimer != NULL, false);

		m_pTimer->Stop();

		return true;
	}

	bool Animation::Stoping()
	{
		GAF_ASSERT_R(m_pTimer != NULL, false);

		m_pTimer->Stop();

		return true;
	}

	void Animation::OnRun(RunBase* pRunBase)
	{
		if(pRunBase == m_pTimer)
		{
			ATTRIB(State) = GAF_PLAYER_RUN;

			TimeTicks _ttNow = GAF_pTHEPLATFORM->GetTimeTicks();
			TimeTicks _ttUsed = _ttNow - m_ttLast;

			bool _isContinue = OnUpdate(_ttUsed);

			m_ttLast = _ttNow;

			if(m_pResponse != NULL)
			{
				this->m_pResponse->OnPlayerResponse(this, GAF_PLAYER_RUN);
			}

			if(!_isContinue)
			{
				Stop();
			}
		}
	}
}
